package com.yyfax.ph.personal.authority.bankcard.presenter;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.personal.authority.bankcard.model.UnbindCardModel;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by ying_he on 2017/5/23.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class UnbindCardPresenter extends Presenter<UnbindCardModel, UnbindCardPresenter.View> {

	private static final String TAG = UnbindCardPresenter.class.getSimpleName();
	private static final String UNBIND_SUCCESS_TIPS = "解绑成功";
	private static final String UNBIND_FAILED_TIPS = "解绑失败";

	public static class Generator implements PresenterFactory<UnbindCardPresenter> {
		@Override
		public UnbindCardPresenter create() {
			return new UnbindCardPresenter();
		}
	}

	private View mView;

	@Override
	public void attach(UnbindCardPresenter.View view) {
		mView = view;
		mModel = new UnbindCardModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void unbindCardSuccess(UnbindCardModel.UnbindCardBean bean);

		void unbindCardFailed(String tips);

		void checkUnbindSuccess(String tips);// 10019:30天内未解绑过银行卡

		void checkUnbindFailed(String tips);//获取失败, 10018:30天内解绑过银行卡, 10020:未绑定银行卡
	}

	public void checkUnbind() {
		execute(UnbindCardModel.MSG_CHECK_UNBIND);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void checkUnbindResult(UnbindCardModel.CheckUnbindResult result) {
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E031, result.getStatus());
		if (isResponseSuccess(result) && "10019".equals(result.getContent().code)) {// 10019:30天内未解绑过银行卡
			mView.checkUnbindSuccess(result.getMsg());
		} else {
			String tips = result.getMsg();
			mView.checkUnbindFailed(TextUtils.isEmpty(tips) ? UNBIND_FAILED_TIPS : tips);
		}
	}

	public void unbindCard() {
		execute(UnbindCardModel.MSG_UNBIND_CARD);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void unbindCardResult(UnbindCardModel.UnbindCardResult result) {
		StatService.onEventSecondKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E031, result.getStatus());
		if (isResponseSuccess(result)) {
			mView.unbindCardSuccess(result.getContent());
		} else {
			String tips = result.getMsg();
			mView.unbindCardFailed(TextUtils.isEmpty(tips) ? UNBIND_FAILED_TIPS : tips);
		}
	}
}
